package org.crumbleworks.crumbchat.client.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import org.crumbleworks.crumbchat.client.controller.ClientControllerInterface;

public class ChatFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    private ClientControllerInterface controller;

    private MenuBar menubar;

    private ChatPanel chatPanel;
    private LoginPanel loginPanel;

    private JSeparator horizontalSeparator;

    private String iconPath = "gfx/chat_icon.png";

    public ChatFrame(ClientControllerInterface controller) {
        this.controller = controller;

        setLookAndFeel();

        setTitle("crumbchat by \u00a9rumbleWorks");
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents();
        placeComponents();

        setIconImage(Toolkit.getDefaultToolkit().getImage(iconPath));

        setPosition();
        pack();
        setVisible(true);
        setResizable(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setPosition() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(((int) screen.getWidth() / 2) - this.getWidth(), ((int) screen.getHeight() / 2) - this.getHeight());
    }

    private void initComponents() {
        menubar = new MenuBar(controller);

        loginPanel = new LoginPanel(controller);
        chatPanel = new ChatPanel(controller);

        horizontalSeparator = new JSeparator(JSeparator.HORIZONTAL);
    }

    private void placeComponents() {
        setJMenuBar(menubar);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        // LoginPanel
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(loginPanel, gbc);

        // Separator
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(horizontalSeparator, gbc);

        // ChatPanel
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(chatPanel, gbc);
    }

    public ChatPanel getChatPanel() {
        return chatPanel;
    }

    public LoginPanel getLoginPanel() {
        return loginPanel;
    }
}
